package com.itcast.activiti.controller;

import cn.itcast.ic.common.bean.Response;
import com.alibaba.fastjson.JSONObject;
import com.itcast.activiti.domain.StartProcessInstance;
import com.itcast.activiti.domain.TaskStart;
import com.itcast.activiti.service.IProcessRuntimeService;
import com.itcast.activiti.service.ITaskRuntimeService;
import com.itcast.activiti.utils.ActivitiPage;
import org.activiti.engine.delegate.BaseExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * 启动流程实例
 */
@RestController
@RequestMapping("/process")
public class ProcessController {
    @Autowired
    private IProcessRuntimeService iProcessRuntimeService;
    @Autowired
    private ITaskRuntimeService iTaskRuntimeService;

    /**
     * 校验流程审批人
     *
     * @param startProcessInstance
     * @return
     */
    @PostMapping("/check/approve")
    public Response checkApprove(@RequestBody StartProcessInstance startProcessInstance) {
        return Response.success(iProcessRuntimeService.checkApprove(startProcessInstance));
    }

    /**
     * 获取当前流程实例
     *
     * @param businessKey 业务标识，通常为业务表的主键，业务标识和流程实例一一对应。
     * @return
     */
    @GetMapping(value = "/{businessKey}")
    public Response<Map<String, Object>> getProcessInstanceMap(@PathVariable("businessKey") String businessKey, @RequestParam(required = false) String tenantId) {
        return Response.success(iProcessRuntimeService.startProcessInstanceResultMap(iProcessRuntimeService.getProcessInstanceByBusinessKey(businessKey, tenantId)));
    }

    /**
     * 获取流程实例
     *
     * @return
     */
    @PostMapping(value = "/list")
    public Response listProcessInstancePage(@RequestBody ActivitiPage activitiPage) {
        return Response.success(iProcessRuntimeService.listProcessInstancePage(activitiPage));
    }

    /**
     * 启动流程
     *
     * @param startProcessInstance 流程实体
     * @return
     */
    @PostMapping(path = "/start")
    public Response<Map<String, Object>> startProcessInstance(@RequestBody StartProcessInstance startProcessInstance) {
        return Response.success(iProcessRuntimeService.startProcessInstance(startProcessInstance));
    }

    /**
     * 发起审批流程实例
     *
     * @param startProcessInstance
     * @return
     */
    @PostMapping("/start/apply")
    public Response startProcessApply(@RequestBody StartProcessInstance startProcessInstance) {
        return Response.success(iProcessRuntimeService.startProcessApply(startProcessInstance));
    }

    /**
     * 发起审批流程实例-完成申请
     *
     * @param startProcessInstance
     * @return
     */
    @PostMapping(path = "/task/apply")
    public Response<Map<String, Object>> startProcessUserCompleteTaskApply(@RequestBody StartProcessInstance startProcessInstance) {
        Map<String, Object> map = iProcessRuntimeService.startProcessApply(startProcessInstance);
        String businessKey = startProcessInstance.getBusinessKey();
        String assignee = startProcessInstance.getUserName();
        Integer applyType = 1;
        TaskStart taskStart = new TaskStart();
        taskStart.setAudit(true);
        taskStart.setComment("申请");
        taskStart.setPassbackParam(startProcessInstance.getPassbackParam());
        taskStart.setVariables(startProcessInstance.getVariables());
        taskStart.setEvent(BaseExecutionListener.EVENTNAME_TAKE);
        String taskId = iTaskRuntimeService.completeTaskApply(startProcessInstance.getTenantId(), businessKey, assignee, taskStart, applyType);
        map.put("taskId", taskId);
        return Response.success(map);
    }

    /**
     * 发起审批流程实例-完成申请(自动完成审批人相同的后面节点)
     *
     * @param startProcessInstance
     * @return
     */
    @PostMapping(path = "/task/auto/next/apply")
    public Response<Map<String, Object>> startProcessUserAutoCompleteTaskApply(@RequestBody StartProcessInstance startProcessInstance) {
        startProcessInstance.setAutoNextComplete(Boolean.TRUE);
        Map<String, Object> map = iProcessRuntimeService.startProcessApply(startProcessInstance);
        String businessKey = startProcessInstance.getBusinessKey();
        String assignee = startProcessInstance.getUserName();
        Integer applyType = 1;
        TaskStart taskStart = new TaskStart();
        taskStart.setAudit(true);
        taskStart.setComment("申请");
        taskStart.setPassbackParam(startProcessInstance.getPassbackParam());
        taskStart.setVariables(startProcessInstance.getVariables());
        taskStart.setEvent(BaseExecutionListener.EVENTNAME_TAKE);
        String taskId = iTaskRuntimeService.autoNextCompleteTaskApply(startProcessInstance.getTenantId(), businessKey, assignee, taskStart, applyType, null);
        map.put("taskId", taskId);
        return Response.success(map);
    }

    /**
     * 流程挂起
     *
     * @param businessKey 业务标识，通常为业务表的主键，业务标识和流程实例一一对应。
     * @return
     */
    @GetMapping(value = "/suspend/{businessKey}")
    public Response<Map<String, Object>> processSuspend(@PathVariable("businessKey") String businessKey, @RequestParam(required = false) String tenantId) {
        return Response.success(iProcessRuntimeService.processSuspend(businessKey, tenantId));
    }

    /**
     * 流程激活
     *
     * @param businessKey 业务标识，通常为业务表的主键，业务标识和流程实例一一对应。
     * @return
     */
    @GetMapping(value = "/activate/{businessKey}")
    public Response<Map<String, Object>> processActivate(@PathVariable("businessKey") String businessKey, @RequestParam(required = false) String tenantId) {
        return Response.success(iProcessRuntimeService.processActivate(businessKey, tenantId));
    }

    /**
     * 获取流程节点信息
     *
     * @param businessKey 业务标识，通常为业务表的主键，业务标识和流程实例一一对应。
     * @return
     */
    @GetMapping(value = "/tasks/{businessKey}")
    public Response<ActivitiPage> tasks(@PathVariable("businessKey") String businessKey,
                                        @RequestParam(required = false) String tenantId) {
        ActivitiPage activitiPage = new ActivitiPage();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("businessKey", businessKey);
        jsonObject.put("tenantId", tenantId);
        activitiPage.setCondition(jsonObject);
        return Response.success(iProcessRuntimeService.tasks(activitiPage));
    }

    /**
     * 获取流程审批人信息
     *
     * @param businessKey 业务标识，通常为业务表的主键，业务标识和流程实例一一对应。
     * @return
     */
    @GetMapping(value = "/assignees/{businessKey}")
    public Response<Set<String>> assignees(@PathVariable("businessKey") String businessKey,
                                           @RequestParam(required = false) String tenantId) {
        ActivitiPage activitiPage = new ActivitiPage();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("businessKey", businessKey);
        jsonObject.put("tenantId", tenantId);
        activitiPage.setCondition(jsonObject);
        return Response.success(iProcessRuntimeService.assignees(businessKey, tenantId));
    }


}
